package com.hl.card.modular.packBack.controller;

import com.hl.card.common.controller.BaseController;
import com.hl.card.common.persistence.model.Enrolment;
import com.hl.card.modular.packBack.service.SuperPartnerActiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/superPartnerActive")
@Controller
public class SuperPartnerActiveController extends BaseController {



    private String PREFIX = "/packback/superPartnerActive/";


    @Autowired
    SuperPartnerActiveService superPartnerActiveService;

    @RequestMapping("")
    public String index(){
        return PREFIX+"superPartnerActive.html";
    }

    @RequestMapping("/upload")
    @ResponseBody
    public Object handleFileUpload(@RequestParam("file") MultipartFile file) throws FileNotFoundException {
        List<Enrolment> enrolments=new ArrayList<>();
        Map<String,Object> res = new HashMap<>();
        //上传错误信息
        String erroMsg="";
        Map<String, Object> result=superPartnerActiveService.transFile(file);
        if(result.get("code").equals("1")){
            enrolments = (List<Enrolment>) result.get("enrolments");

            Map<String,Object> map = superPartnerActiveService.insertBatch(enrolments);
            res.put("code",200);
            res.put("msg","插入成功");
            res.put("data",map);
        }else{
            erroMsg= (String) result.get("msg");
            res.put("code",201);
            res.put("msg",erroMsg);
        }
        return res;
    }



    @RequestMapping("/list")
    @ResponseBody
    public Object list(Integer limit,Integer offset,String phone,Integer status){
        Map<String, Object> map = new HashMap<>();
        offset = offset * limit;
        map.put("limit",limit);
        map.put("offset",offset);
        map.put("phone",phone);
        map.put("status",status);
        List<Enrolment> list= superPartnerActiveService.selectList(map);
        return list;

    }

    @RequestMapping("/sendmsg")
    @ResponseBody
    public Object sendmsg(ArrayList<Enrolment> id){
        superPartnerActiveService.sendmsg(id);
        return super.SUCCESS_TIP;

    }
    @RequestMapping(value = "/del",method = RequestMethod.POST)
    @ResponseBody
    public Object del(@RequestParam(name="id[]") int[] id){
        for(int i =0;i < id.length;i++) {
            superPartnerActiveService.delete(id[i]);
        }
        return super.SUCCESS_TIP;
    }









}


